<?php

namespace app\speedDating\services\common;

use app\speedDating\interfaces\KeyName;
use app\speedDating\model\SddUser;
use think\facade\Cache;

class JWTAuthUserService extends JWTService
{
    /**
     * 校验通过用户注册入缓存
     * @param $userMain
     * @return void
     * @throws \Psr\SimpleCache\InvalidArgumentException
     */
    public static function register($userMain)
    {

        $authorization = request()->header(KeyName::AUTHORIZATION);
        list(, , $signature) = explode('.', $authorization);

        $uniqueKey = KeyName::MEMBER_CACHE_JWT_INFO . md5($signature);
        Cache::store(KeyName::CACHE_DRIVER)->set($uniqueKey, $userMain, 3600);
    }

    /**
     * 获取当前访问的用户信息
     * @return mixed
     * @throws \Psr\SimpleCache\InvalidArgumentException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function user()
    {

        $authorization = request()->header(KeyName::AUTHORIZATION);

        list(, , $signature) = explode('.', $authorization);

        $uniqueKey = KeyName::MEMBER_CACHE_JWT_INFO . md5($signature);
        $userMain = Cache::store(KeyName::CACHE_DRIVER)->get($uniqueKey);

        return SddUser::getSimpleMainByID($userMain['u_id']);
    }
}